<?php
require_once "../class/fenajax.class.php";
include "../dyn/connexion.php";

$f = new fenAjax("Formulaire admin",0);

if(isset($_POST['mode']) and isset($_POST['mdp']) and isset($_POST['nom']) and isset($_POST['prenom']) and isset($_POST['pseudo']) and isset($_POST['email']) and isset($_POST['adresse']) and isset($_POST['statut']) and isset($_POST['mdp_confirm']) and isset($_POST['id_user']))
{
	$mode = $_POST['mode'];
	$mdp = $_POST['mdp'];
	$nom = $_POST['nom'];
	$prenom = $_POST['prenom'];
	$pseudo = $_POST['pseudo'];
	$email = $_POST['email'];
	$adresse = $_POST['adresse'];
	$statut = $_POST['statut'];
	$mdp_confirm = $_POST['mdp_confirm'];
	$id_user = $_POST['id_user'];	
	
	
	$id = $_SESSION['user_id'];
	
	if( !in_array($mode,array(1,2,3,4,5,6)))
		$f->ajaxErreur("Fonction non supportée","Ce mode n'est pas supporté pour ce module");
	
	if($mode == 1){							$header = "Modifier le mot de passe"; 		$nomAction = "Modifier";	$disable = ""; }
	elseif($mode == 2 or $mode == 6){		$header = "Modifier les informations"; 		$nomAction = "Modifier"; 	$disable = ""; }
	elseif($mode == 3){ 					$header = "Ajouter un utilisateur"; 		$nomAction = "Ajouter"; 	$disable = ""; }
	elseif($mode == 4){						$header = "Modifier  un utilisateur"; 		$nomAction = "Modifier"; 	$disable = ""; }
	elseif($mode == 5){						$header = "Supprimer  un utilisateur"; 		$nomAction = "Supprimer"; 	$disable = ""; }
	

	
	$f->retourAjax["boutton"] = $nomAction;
	$f->retourAjax["header"] = $header;
	
	
	$form = "<div id=\"feedback\"></div><br />";
	
	if ($mode == 5) {
		$form .= BMInfo("La supression d'un utilisateur est définitive");
	}
			
	$form .= "<form action=\"#\" method=\"post\" onSubmit=\"return actionAdmin($(this));\">";
	
	if($mode == 1) {
		
		$form .= "<br /><label class=\"formInfos2\" for=\"mdp\">Nouveau mot de passe :</label>
				<input class=\"ui-corner-all\" id=\"mdp\" type=\"password\" value=\"".s($mdp)."\" name=\"mdp\" $disable/><br /><br />
				
				<br /><label class=\"formInfos2\" for=\"mdp_confirm\">Confirmer mot de passe :</label>
				<input class=\"ui-corner-all\" id=\"mdp_confirm\" type=\"password\" value=\"".s($mdp_confirm)."\" name=\"mdp_confirm\" $disable/><br /><br />
				
				<input type=\"hidden\" name=\"nom\" value=\"".$nom."\" />
				<input type=\"hidden\" name=\"prenom\" value=\"".$prenom."\" />
				<input type=\"hidden\" name=\"pseudo\" value=\"".$pseudo."\" />
				<input type=\"hidden\" name=\"email\" value=\"".$email."\" />
				<input type=\"hidden\" name=\"adresse\" value=\"".$adresse."\" />
				<input type=\"hidden\" name=\"statut\" value=\"".$statut."\" />
				<input type=\"hidden\" name=\"id_user\" value=\"".$id_user."\" />
		";
	}
	
	if($mode == 2 or $mode == 4 or $mode == 6) {
		
		if ($mode == 2 or $mode == 6) {
			$res = mysql_query("SELECT * FROM Utilisateur WHERE  id = '".$id."' ;");
		}
		if ($mode == 4) {
			$res = mysql_query("SELECT * FROM Utilisateur WHERE  id = '".$id_user."' ;");
		} 
		
		while($row = mysql_fetch_array($res)) {
			
			$nom_statut = "";
			if ($row['statut'] == 4) {
				$nom_statut = "Administrateur";
			}
			else if ($row['statut'] == 3) {
				$nom_statut = "Animateur";
			}
			else if ($row['statut'] == 2) {
				$nom_statut = "Adhérent";
			}
			else if ($row['statut'] == 1) {
				$nom_statut = "Contributeur";
			}
			else {
				$nom_statut = "Erreur statut";
			}
				
			
			$form .= "<br /><label class=\"formInfos\" for=\"nom\">Nom :</label>
					<input class=\"ui-corner-all\" id=\"nom\" type=\"text\" value=\"".$row['nom']."\" name=\"nom\" $disable/><br /><br />
					
					<br /><label class=\"formInfos\" for=\"prenom\">Prenom :</label>
					<input class=\"ui-corner-all\" id=\"prenom\" type=\"text\" value=\"".$row['prenom']."\" name=\"prenom\" $disable/><br /><br />
					
					<br /><label class=\"formInfos\" for=\"pseudo\">Pseudo :</label>
					<input class=\"ui-corner-all\" id=\"pseudo\" type=\"text\" value=\"".$row['pseudo']."\" name=\"pseudo\" $disable/><br /><br />
										
					<br /><label class=\"formInfos\" for=\"email\">Email :</label>
					<input class=\"ui-corner-all\" id=\"email\" type=\"email\" value=\"".$row['email']."\" name=\"email\" $disable/><br /><br />
					
					<br /><label class=\"formInfos\" for=\"adresse\">Adresse :</label>
					<input class=\"ui-corner-all\" id=\"adresse\" type=\"text\" value=\"".$row['adresse']."\" name=\"adresse\" $disable/><br /><br />
			";
					
			
			if ($mode == 2 or $mode == 4) {
				$form .= "<br /><label class=\"formInfos\" for=\"statut\">Statut :</label>
					<select id=\"statut\" name=\"statut\" $disable>
					
					<option value=\"1\" ".($nom_statut == "Contributeur" ? "selected=\"selected\"":"").">Contributeur</option>
					<option value=\"2\" ".($nom_statut == "Adhérent" ? "selected=\"selected\"":"").">Adhérent</option>
					<option value=\"3\" ".($nom_statut == "Animateur" ? "selected=\"selected\"":"").">Animateur</option>
					<option value=\"4\" ".($nom_statut == "Administrateur" ? "selected=\"selected\"":"").">Administrateur</option>
					
					</select>
			";
			}
			
			if ($mode == 6) {
				
				$form .= "<input type=\"hidden\" name=\"statut\" value=\"".$row['statut']."\" />";
			}
					
			$form .= "<input type=\"hidden\" name=\"mdp\" value=\"".$mdp."\" />
					<input type=\"hidden\" name=\"mdp_confirm\" value=\"".$mdp_confirm."\" />
					<input type=\"hidden\" name=\"id_user\" value=\"".$id_user."\" />

			";
		}
	}
	

	if($mode == 3) {
		$form .= "<br /><label class=\"formInfos\" for=\"nom\">Nom :</label>
					<input class=\"ui-corner-all\" id=\"nom\" type=\"text\" value=\"".s($nom)."\" name=\"nom\" $disable/><br /><br />
					
					<br /><label class=\"formInfos\" for=\"prenom\">Prenom :</label>
					<input class=\"ui-corner-all\" id=\"prenom\" type=\"text\" value=\"".s($prenom)."\" name=\"prenom\" $disable/><br /><br />
					
					<br /><label class=\"formInfos\" for=\"pseudo\">Pseudo :</label>
					<input class=\"ui-corner-all\" id=\"pseudo\" type=\"text\" value=\"".s($pseudo)."\" name=\"pseudo\" $disable/><br /><br />
					
					<br /><label class=\"formInfos\" for=\"mdp\">Mot de passe :</label>
					<input class=\"ui-corner-all\" id=\"mdp\" type=\"password\" value=\"".s($mdp)."\" name=\"mdp\" $disable/><br /><br />
										
					<br /><label class=\"formInfos\" for=\"email\">Email :</label>
					<input class=\"ui-corner-all\" id=\"email\" type=\"email\" value=\"".s($email)."\" name=\"email\" $disable/><br /><br />
					
					<br /><label class=\"formInfos\" for=\"adresse\">Adresse :</label>
					<input class=\"ui-corner-all\" id=\"adresse\" type=\"text\" value=\"".s($adresse)."\" name=\"adresse\" $disable/><br /><br />
					
					<br /><label class=\"formInfos\" for=\"statut\">Statut :</label>
					<select id=\"statut\" name=\"statut\" $disable>
					
					<option value=\"1\" selected=\"selected\">Contributeur</option>
					<option value=\"2\">Adhérent</option>
					<option value=\"3\">Animateur</option>
					<option value=\"4\">Administrateur</option>
					
					</select>
					
					<input type=\"hidden\" name=\"mdp_confirm\" value=\"".$mdp_confirm."\" />
					<input type=\"hidden\" name=\"id_user\" value=\"".$id_user."\" />
					
					";
	}
	
	
	if($mode == 5) {
		$form .= "<br /><label class=\"formInfos\" for=\"id_user\">Utilisateur :</label>
					<select id=\"id_user\" name=\"id_user\" $disable>";
					
		$res = mysql_query("SELECT id,nom, prenom FROM Utilisateur ORDER BY nom;");
		$prems = true;
		
		while($row = mysql_fetch_array($res)) {
			$form .= "<option value=\"".$row['id']."\" ".($prems ? "selected=\"selected\"":"").">".$row['nom']." ".$row['prenom']."</option>";
			$prems = false;
		}
		
		$form .= "</select>";
		
		$form .= " <input type=\"hidden\" name=\"nom\" value=\"".$nom."\" />
			<input type=\"hidden\" name=\"prenom\" value=\"".$prenom."\" />
			<input type=\"hidden\" name=\"pseudo\" value=\"".$pseudo."\" />
			<input type=\"hidden\" name=\"email\" value=\"".$email."\" />
			<input type=\"hidden\" name=\"adresse\" value=\"".$adresse."\" />
			<input type=\"hidden\" name=\"statut\" value=\"".$statut."\" />
			<input type=\"hidden\" name=\"mdp\" value=\"".$mdp."\" />
			<input type=\"hidden\" name=\"mdp_confirm\" value=\"".$mdp_confirm."\" />";
		
	}
		
	
	$form .= "<input type=\"hidden\" name=\"mode\" value=\"".$mode."\" />
	</form>";
	
	$f->retourAjax["erreur"] = false;
	$f->retourAjax["msg"] = $form;
}

$f->endAjax();
?>